﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.DataProvider;
using CuaHangMayTinh.Utilities;
using System.Data;

namespace CuaHangMayTinh.DataAccessLayer
{
    /// <summary>
    /// 
    /// </summary>
    public class XuatKhoTraHangDA
    {
        public XuatKhoTraHangDA()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(XuatKhoTraHangEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("MaNV", entity.MaNV);
                pb.AddParameter("MaNCC", entity.MaNCC);
                pb.AddParameter("NgayLap", entity.NgayLap);
                pb.AddParameter("GhiChu", entity.GhiChu);
                pb.AddParameter("TongTien", entity.TongTien);

                return (int)DBFactory.Database.ExecuteNonQuery("XuatKho_TraHang_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conn"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        int InsertTS(XuatKhoTraHangEntity entity, DbConnector conn, System.Data.Common.DbTransaction tran)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("MaNV", entity.MaNV);
                pb.AddParameter("MaNCC", entity.MaNCC);
                pb.AddParameter("NgayLap", entity.NgayLap);
                pb.AddParameter("GhiChu", entity.GhiChu);
                pb.AddParameter("TongTien", entity.TongTien);

                return (int)DBFactory.Database.ExecuteNonQueryGetID("XuatKho_TraHang_Insert", pb.Parameters, conn, tran);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int InsertTS(XuatKhoTraHangEntity entity)
        {
            try
            {
                DbConnector conn = DBFactory.Database.GetConnector();
                System.Data.Common.DbTransaction tran = DBFactory.Database.CreateTransaction(conn);

                int IDXK = this.InsertTS(entity, conn, tran);
                if (IDXK == 0)
                    return 0;

                int countModel = entity.ListModel.Count;

                for (int i = 0; i < countModel; i++)
                {
                    //
                    ModelMatHangEntity model = entity.ListModel[i];
                    //ModelMatHangDA mda = new ModelMatHangDA();
                    //int IDModel = mda.GetID(model.HangSX, model.Model);
                    int IDModel = model.MaModelMH;
                    //
                    ChiTietXuatKhoTraHang chitiet = entity.ListChiTiet[i];
                    chitiet.Ma_XK_TH = IDXK;
                    chitiet.MaModelMH = IDModel;

                    if (ChiTietXuatKhoTraHangDA.Insert(chitiet, conn, tran) == 0)
                        return 0;

                    int countmathang = model.ListMatHang.Count;
                    for (int j = 0; j < countmathang; j++)
                    {
                        MatHangEntity mathang = model.ListMatHang[j];
                        mathang.MaPhieuXuatKhoTraHang = IDXK;
                        mathang.TinhTrang = 4;
                        if (!MatHangDA.UpDateDaTraTS(mathang, conn, tran))
                            return 0;
                    }

                }
                DBFactory.Database.CommitTransaction(conn, tran);
                return IDXK;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="IDSubGroupUser"></param>
        /// <param name="Quyen"></param>
        /// <returns></returns>
        public DataTable Find_All(bool View)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("View", View);

                dt = DBFactory.Database.FillDataTable("XuatKhoTraHang_FindAll", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataTable GetMaHDMax()
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();

                dt = DBFactory.Database.FillDataTable("XuatKhoTraHang_GetMaHDMax", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaNhapHang"></param>
        /// <returns></returns>
        DataTable GetInfoXuatKhoTraHang_ID(int MaXuatKhoTraHang)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaXuatKhoTraHang", MaXuatKhoTraHang);

                dt = DBFactory.Database.FillDataTable("XuatKhoTraHang_Find_MaXuatKhoTraHang", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }


        public XuatKhoTraHangEntity GetAllInfo_ID(int MaXuatKhoTraHang)
        {
            try
            {
                List<XuatKhoTraHangEntity> xuatkhos = ConvertToList(this.GetInfoXuatKhoTraHang_ID(MaXuatKhoTraHang));

                XuatKhoTraHangEntity xuatkho = xuatkhos[0];

                ChiTietXuatKhoTraHangDA chitietda = new ChiTietXuatKhoTraHangDA();

                List<ChiTietXuatKhoTraHang> chitiets = chitietda.ConvertToList(chitietda.GetInfo_ByMaXuatKhoTraHang(MaXuatKhoTraHang));

                List<ModelMatHangEntity> models = new List<ModelMatHangEntity>();

                ModelMatHangDA modelda = new ModelMatHangDA();

                MatHangDA mathangda = new MatHangDA();

                foreach (var chitiet in chitiets)
                {
                    ModelMatHangEntity model = modelda.ConvertToList(modelda.Find_ID(chitiet.MaModelMH, false))[0];
                    models.Add(model);
                }

                foreach (var model in models)
                {
                    List<MatHangEntity> mathangs = new List<MatHangEntity>();
                    mathangs = mathangda.ConvertToList(mathangda.Find_MaModelMH_MaXuatKhoTraHang(model.MaModelMH, MaXuatKhoTraHang, false));
                    model.ListMatHang = mathangs;
                }

                xuatkho.ListChiTiet = chitiets;

                xuatkho.ListModel = models;
               
                return xuatkho;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<XuatKhoTraHangEntity> ConvertToList(DataTable dt)
        {
            List<XuatKhoTraHangEntity> list = new List<XuatKhoTraHangEntity>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int ma = (int)dt.Rows[i][0];
                string mahd = dt.Rows[i][1].ToString();
                int manv = (int)dt.Rows[i][2];
                int mancc = (int)dt.Rows[i][3];
                DateTime ngaylap = (DateTime)dt.Rows[i][4];
                decimal tong = (decimal)dt.Rows[i][5];
                string ghichu = dt.Rows[i][6].ToString();

                XuatKhoTraHangEntity a = new XuatKhoTraHangEntity(ma, mahd, manv, mancc, ngaylap, tong, ghichu);
                list.Add(a);
            }
            return list;
        }
    }
}
